package cn.com.dhcc.turbo.web.controller.stat;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.dhcc.app.core.base.BaseController;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.pub.core.annotation.TurboMenu;
import cn.com.dhcc.app.pub.core.annotation.TurboMenu.LEAF_MENU;
import cn.com.dhcc.turbo.entity.registry.DomainInfo;
import cn.com.dhcc.turbo.entity.registry.NodeInfo;
import cn.com.dhcc.turbo.entity.stat.FreqStat;
import cn.com.dhcc.turbo.entity.sysmgr.UserInfo;
import cn.com.dhcc.turbo.service.registry.DomainInfoService;
import cn.com.dhcc.turbo.service.registry.NodeInfoService;
import cn.com.dhcc.turbo.service.stat.FreqStatService;

@Controller
@RequestMapping("/stat")
public class ExchStatController extends BaseController {

	@SuppressWarnings("deprecation")
	private static Logger logger = Log.getExistLogger(LOGTYPE.TURBO);

	@Autowired
	private NodeInfoService nodeService;
	
	@Autowired
	private DomainInfoService doMainService;
	
	@Autowired
	private FreqStatService freqStatService;

	/**
	 * 
	 * @param model
	 * @param httpSession
	 * @return
	 */
	@TurboMenu(belongToMenu = LEAF_MENU.EXCH_TOTAL)
	@RequestMapping(value = "/exch_total")
	public String exchTotal(Model model, HttpSession httpSession) {
		try {
			NodeInfo node = nodeService.getLocalNode();
		//	List<SysInfo> sysList = sysInfoService.getLocalSysInfoList();
			List<DomainInfo> domainList = doMainService.getLocalDomain();

			model.addAttribute("node", node);
			model.addAttribute("sysList", null);
			model.addAttribute("domainList", domainList);
			UserInfo user = (UserInfo)getCurrentUserInfo(httpSession);
			if (user != null) {
				model.addAttribute("nodeAdmin", user.getUserType());
				model.addAttribute("ownId", user.getOwnId());
			}
		} catch (Exception e) {
			logger.error("统计失败", e);
			return toAlertPage(model, e.getMessage());
		}
		return "stat/exch_total";
	}

	@TurboMenu(belongToMenu = LEAF_MENU.EXCH_FREQ)
	@RequestMapping(value = "/exch_freq")
	public String exchFreq(Model model, HttpSession httpSession) {
		try {
			NodeInfo node = nodeService.getLocalNode();
		//	List<SysInfo> sysList = sysInfoService.getLocalSysInfoList();

			model.addAttribute("node", node);
			model.addAttribute("sysList", null);
			UserInfo user = (UserInfo)getCurrentUserInfo(httpSession);
			if (user != null) {
				model.addAttribute("nodeAdmin", user.getUserType());
				model.addAttribute("ownId", user.getOwnId());
			}
		} catch (Exception e) {
			logger.error("统计失败", e);
			return toAlertPage(model, e.getMessage());
		}
		return "stat/exch_freq";
	}

	/**
	 * 按节点统计的交换的全部统计值
	 * @param model
	 * @param timeType 统计时间类型 年 月
	 * @param level 统计级别 节 系统
	 * @param ownerid 所属id
	 * @param time 统计时间
	 * @return
	 * @throws DaoException
	 */
	@RequestMapping(value = "/exch_freq_node_list")
	public String freqNodeTotalList(Model model, String timeType,
			String level, String ownerid, String time) throws DaoException {
		String msg = "";
		try {
			List<FreqStat> list = freqStatService.getFreqStatList(ownerid, time, timeType, level);
			model.addAttribute("list", list);
			return "stat/exch_freq_node_list";
		} catch (ServiceException e) {
			msg = "数据发送量实时监控失败";
			return toAlertPage(model, msg + e.getMessage());
		}
	}

	/**
	 * 按系统统计的交换的全部统计值
	 * @param model
	 * @param timeType 统计时间类型 年 月
	 * @param level 统计级别 节 系统
	 * @param ownerid 所属id
	 * @param time 统计时间
	 * @return
	 * @throws DaoException
	 */
	@RequestMapping(value = "/exch_freq_sys_list")
	public String freqSysTotalList(Model model, String timeType,
			String level, String ownerid, String time) throws DaoException {
		String msg = "";
		try {
			List<FreqStat> list = freqStatService.getFreqStatList(ownerid, time, timeType, level);
			model.addAttribute("list", list);
			return "stat/exch_freq_sys_list";
	} catch (ServiceException e) {
		msg = "数据发送量实时监控失败";
		return toAlertPage(model, msg + e.getMessage());
	}
	}
	
	/**
	 * 
	 * @param model
	 * @param timeType 统计时间类型　年　　月
	 * @param level　统计级别　节点　域　系统
	 * @param ownerid　所属id
	 * @param time 统计时间
	 * @param dataTransType　数据传输类型　发送　接收
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/stat_total")
	public Map<String, Object> statTotal(Model model, String timeType,
			String level, String ownerid, String time, String dataTransType) {
		String msg = "";
		//try {
			WhereCondition condition = new WhereCondition();

			if (timeType.equals("month")) {//按月，统计一个月内每一天数据
				condition.and();
				if (dataTransType.equals("send")) {// 发送数据
					if (level.equals("node")) {
						condition.eq("t2.node_id", ownerid);
					} else if (level.equals("domain")) {
						condition.eq("t2.domain_id", ownerid);
					} else {
						condition.eq("t1.send_sysid", ownerid);
					}
				} else {//接收数据
					if (level.equals("node")) {
						condition.eq("t3.node_id", ownerid);
					} else if (level.equals("domain")) {
						condition.eq("t3.domain_id", ownerid);
					} else {
						condition.eq("t1.recv_sysid", ownerid);
					}
				}
				condition.and().eq("to_char(t1.st_time,'yyyy-mm')", time);
				if (level.equals("node")) {
					condition.groupBy("to_char(t1.st_time,'dd')");
				} else if (level.equals("domain")) {
					condition.groupBy("t2.domain_id,to_char(t1.st_time,'dd')");
				} else{
					condition.groupBy("to_char(t1.st_time,'dd')");
				}
				condition.orderBy("to_char(t1.st_time,'dd')");
				//msg = exchStatService.getSysStatMonth(condition);
			} else {//按年，统计一年内十二个月数据
				condition.and();
				if (dataTransType.equals("send")) {// 发送数据
					if (level.equals("node")) {
						condition.eq("t2.node_id", ownerid);
					} else if (level.equals("domain")) {
						condition.eq("t2.domain_id", ownerid);
					} else {
						condition.eq("t1.send_sysid", ownerid);
					}
				} else {//接收数据
					if (level.equals("node")) {
						condition.eq("t3.node_id", ownerid);
					} else if (level.equals("domain")) {
						condition.eq("t3.domain_id", ownerid);
					} else {
						condition.eq("t1.recv_sysid", ownerid);
					}
				}
				condition.and().eq("to_char(t1.st_time,'yyyy')", time);
				if (level.equals("node")) {
					condition.groupBy("to_char(t1.st_time,'mm')");
				} else if (level.equals("domain")) {
					condition.groupBy("t2.domain_id,to_char(t1.st_time,'mm')");
				} else{
					condition.groupBy("to_char(t1.st_time,'mm')");
				}
				condition.orderBy("to_char(t1.st_time,'mm')");
			//	msg = exchStatService.getSysStatYear(condition);
			}
			return jsonSuccessDataResult("success", msg);
		/*} catch (ServiceException e) {
			msg = "数据发送量实时监控失败";
			logger.error(msg, e);
			return jsonFailResult(msg);
		}*/
	}
	
	/**
	 * 节点间数据交换统计top10
	 * @param model
	 * @param timeType 统计时间类型 年 月
	 * @param level 统计级别 节 系统
	 * @param ownerid 所属id
	 * @param time 统计时间
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/stat_freq")
	public Map<String, Object> freqTotal(Model model, String timeType,
			String level, String ownerid, String time) {
		String msg = "";
		try {
			msg = freqStatService.getFreqStat(ownerid,time,timeType,level);
			return jsonSuccessDataResult("success", msg);
		} catch (ServiceException e) {
			msg = "数据发送量实时监控失败";
			logger.error(msg, e);
			return jsonFailResult(msg);
		}
	}
}
